/**
 * 
 */
package com.sendMessage.demo.socket.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author 周成瑜
 * 
 */
public class SocketUtil {

	private static String ip = "192.168.1.104";// 设置成服务器IP

	private static int port = 12345;

	private static Socket socket = null;

	/**
	 * 
	 * @param msg 客户端需要发送的消息
	 * @return  服务端响应的消息
	 */
	public static String ClientsendMsg(String msg) {
		 String s2c="";
		try {
			socket = new Socket(ip, port);
			OutputStream os = socket.getOutputStream();
			DataOutputStream bos = new DataOutputStream(os);
			bos.writeUTF(msg);
			bos.flush();
			
			 // 接收服务器端数据 
            InputStream is = socket.getInputStream(); 
            DataInputStream dis = new DataInputStream(is); 
            //System.out.println("客户端接收到服务端消息："+dis.readUTF()); 
            
            //接收到服务端返回的信息
            s2c=dis.readUTF();
            dis.close();
            is.close();
            bos.close();
            os.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return s2c;
	}
	public static void main(String[] args) {
		System.out.println(ClientsendMsg("hahaah"));
	}
}
